package com.leetcode.algorithm.y22.m07.w4;

/**
 * 415. 字符串相加
 * 
 * https://leetcode.cn/problems/add-strings/
 * 
 * @author jie.deng
 *
 */
class Question0415Solution01 {

	public String addStrings(String num1, String num2) {
		if ("0".equals(num1)) {
			return num2;
		}
		if ("0".equals(num2)) {
			return num1;
		}
		int carry = 0;
		StringBuilder builder = new StringBuilder();
		int len1 = num1.length();
		int len2 = num2.length();
		for (int i = 1; i <= len1 || i <= len2; i++) {
			int i1 = (i <= len1) ? num1.charAt(len1 - i) - '0' : 0;
			int i2 = (i <= len2) ? num2.charAt(len2 - i) - '0' : 0;
			int sum = i1 + i2 + carry;
			builder.append(sum % 10);
			carry = sum / 10;
		}
		if (carry > 0) {
			builder.append(carry);
		}
		return builder.reverse().toString();
	}

}